package com.roiland.contentproviderdemo;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button insertBtn;
    private Button queryBtn;
    private Button updateBtn;
    private Button deleteBtn;
    private Button querysBtn;
    private Uri uri = null;
    private  ContentResolver cr = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        uri = Uri.parse("content://com.kingtone.provider/profile");
        cr = getContentResolver();
        initView();
    }

    private void initView()
    {
        insertBtn = (Button) findViewById(R.id.insert);
        insertBtn.setOnClickListener(this);
        queryBtn = (Button) findViewById(R.id.query);
        queryBtn.setOnClickListener(this);
        updateBtn = (Button) findViewById(R.id.update);
        updateBtn.setOnClickListener(this);
        deleteBtn = (Button) findViewById(R.id.delete);
        deleteBtn.setOnClickListener(this);
        querysBtn = (Button) findViewById(R.id.querys);
        querysBtn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        Log.d("select insert btn", v.getId() == R.id.insert?"true":"false");
        switch (v.getId())
        {
            case R.id.insert:
            {
                ContentValues cv = new ContentValues();
                cv.put("name", "张三");
                cr.insert(uri, cv);
                Log.e("contentproviderdemo:", "insert success");
            }
            break;
            case R.id.querys:
            {
                Cursor c = cr.query(uri, null, null, null, null);
                while(c.moveToNext())
                {
                    Log.e("name", "name== " + c.getString(c.getColumnIndex
                            (ContentConstants.COLUMN_NAME)));
                }
                c.close();
            }
            case R.id.query:
            {
                Cursor c = cr.query(uri, null, "_id =?", new String[]{"1"}, null);
                if(c.moveToNext())
                {
                    Log.e("name", "name== " + c.getString(c.getColumnIndex
                            (ContentConstants.COLUMN_NAME)));
                }
                c.close();
            }
            break;
            case R.id.update:
            {
                ContentValues cv = new ContentValues();
                cv.put("name", "李四");
                cr.update(uri, cv, "_id=?", new String[]{"1"});
                Log.e("contentproviderdemo:", "update success");
            }
            break;
            case R.id.delete:
            {
                cr.delete(uri,"_id=?",new String[]{"1"});
                Log.e("contentproviderdemo:", "delete success");
                //this is test
            }
        }
    }
}
